package com.twentyfour.controller;


import com.twentyfour.pojo.*;
import com.twentyfour.service.AdminServiceImpl;
import com.twentyfour.service.LeaderServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    @Qualifier("AdminServiceImpl")
    AdminServiceImpl adminService;

    @Autowired
    @Qualifier("LeaderServiceImpl")
    LeaderServiceImpl leaderService;




    @RequestMapping("/addTeam")
    public void addTeam(String name, String password, HttpSession session, HttpServletRequest request,
                        HttpServletResponse response
                        ) throws IOException {
        //Admin admin = (Admin) session.getAttribute("admin");

        Team team = new Team();
        team.setName(name);
        team.setPassword(password);
        adminService.addTeam(team);
        adminService.queryAllTeam();

        response.sendRedirect(request.getContextPath()+"/toTeamList");
    }


    @RequestMapping("/deleteTeam")
    public void deleteTeam(Integer account,HttpServletRequest request,
                           HttpServletResponse response) throws IOException {
        adminService.deleteTeam(account);

        response.sendRedirect(request.getContextPath()+"/toTeamList");
    }

    @RequestMapping("/updateTeam")
    public void updateTeam(String name,String password, HttpServletRequest request,
                           HttpServletResponse response) throws IOException {
        Team team = new Team();
        team.setName(name);
        team.setPassword(password);

        adminService.updateTeam(team);

        response.sendRedirect(request.getContextPath()+"/toTeamList");
    }

    @RequestMapping("/editGroupNum")
    public void editGroupNum(Integer groupNum, Integer infoId, HttpServletRequest request,
                             HttpServletResponse response,Integer itemId) throws IOException {
        List<GameInformation> gameInformations = adminService.queryAllGameInformation();

        for (GameInformation gameInformation:gameInformations){
            if (gameInformation.getId().equals(infoId)){
                gameInformation.setGroupNum(groupNum);
                adminService.updateGameInformation(gameInformation);
            }
        }


        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+itemId);
    }

    @RequestMapping("/editPlayOrder")
    public void editPlayOrder(Integer playOrder, Integer infoId, HttpServletRequest request,
                              HttpServletResponse response,Integer itemId) throws IOException {
        List<GameInformation> gameInformations = adminService.queryAllGameInformation();

        for (GameInformation gameInformation:gameInformations){
            if (gameInformation.getId().equals(infoId)){
                gameInformation.setPlayOrder(playOrder);
                adminService.updateGameInformation(gameInformation);
            }
        }


        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+itemId);
    }

    @RequestMapping("/addRefereeItem")
    public void addRefereeItem(HttpServletRequest request, HttpServletResponse response,HttpSession session,
                               Integer certainItemId, Integer refereeId,String chiefReferee) throws IOException {

        List<GameInformation> gameInformations = adminService.queryAllGameInformationByItemId(certainItemId);

        for (GameInformation gameInformation:gameInformations){
            RefereeScore refereeScore = new RefereeScore();
            refereeScore.setChiefReferee(chiefReferee);
            refereeScore.setRefereeId(refereeId);
            refereeScore.setGameInfoId(gameInformation.getId());
            adminService.addRefereeScore(refereeScore);
        }

        Referee_Item referee_item = new Referee_Item();
        referee_item.setItemId(certainItemId);
        referee_item.setRefereeId(refereeId);
        adminService.addRefereeItem(referee_item);

        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+certainItemId);

    }

    @RequestMapping("/deleteRefereeItem")
    public void deleteRefereeItem(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                                  @RequestParam("itemId") Integer itemId, @RequestParam("refereeId") Integer refereeId) throws IOException {

        /*List<RefereeScore> refereeScores = adminService.queryAllRefereeScore();
        for(RefereeScore refereeScore:refereeScores){
            if(refereeScore.getRefereeId().equals(refereeId)){
                adminService.deleteRefereeScore(refereeId);
            }
        }*/


        Referee_Item referee_item = new Referee_Item();
        referee_item.setRefereeId(refereeId);
        referee_item.setItemId(itemId);
        adminService.deleteRefereeItem(referee_item);


        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+itemId);
    }

    /*@RequestMapping("/deleteGame")
    public String deleteGame(Integer id){
        adminService.deleteGame(id);
        adminService.queryAllGames();
        return "redirect:toGameInfo";
    }

    @RequestMapping("/addGame")
    public String addGame(){


        return "admin/gameInfo";
    }*/

/*
    @RequestMapping("/editGame")
    public String editGame(Model model,Integer itemId){

        List<GameInformation> gameInformations = adminService.queryGameListByItemId(itemId);
        model.addAttribute("infos",gameInformations);
        model.addAttribute("itemId",itemId);
        List<Referee_Item> referees = adminService.queryAllRefereeByItemId(itemId);
        model.addAttribute("referees",referees);


        return "admin/gameList";
    }

    @RequestMapping("/editPlayOrder")
    public void editPlayOrder(@RequestParam("order")Integer order,@RequestParam("athleteId") Integer athleteId,
                              HttpServletResponse response,HttpServletRequest request,@RequestParam("itemId") Integer itemId) throws IOException {

        List<GameInformation> gameInformations = adminService.queryAllGameInfo();


        for(GameInformation info:gameInformations){
                if (info.getAthleteId().getId().equals(athleteId)){
                    info.setPlayOrder(order);
                    adminService.updateGameInfo(info);
                }
            }

        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+itemId);
    }

    @RequestMapping("/editGroupNum")
    public void editGroupNum( Model model,@RequestParam("group") Integer group,@RequestParam("athleteId") Integer athleteId,@RequestParam("itemId") Integer itemId,
                                HttpServletRequest request,HttpServletResponse response) throws IOException {
        List<GameInformation> gameInformations = adminService.queryAllGameInfo();

        for(GameInformation info:gameInformations){
            if (info.getAthleteId().getId().equals(athleteId)){
                info.setGroupNum(group);
            }
        }
        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+itemId);
    }

*/

/*    @RequestMapping("/addReferee")
    public void addReferee(ModelMap model, @RequestParam("name") String name, @RequestParam("phoneNum") String phoneNum,
                             @RequestParam("itemId") Integer itemId,HttpServletResponse response, HttpServletRequest request) throws IOException {
        Referee_Item referee_item = new Referee_Item();
        List<Item> items = adminService.queryAllItems();
        List<Referee> referees = adminService.queryAllReferee();
        for(Item item:items){
            if (item.getId().equals(itemId)){
                referee_item.setItemId(item);
            }
        }

        for (Referee referee:referees){
            if (referee.getName().equals(name)&&referee.getPhoneNum().equals(phoneNum)){
                referee_item.setRefereeId(referee);
            }
        }


        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+itemId);

    }

    @RequestMapping("/deleteReferee")
    public void deleteReferee(Model model,Integer refereeId,Integer itemId,
                              HttpServletRequest request, HttpServletResponse response) throws IOException {
        adminService.deleteReferee(refereeId);

        response.sendRedirect(request.getContextPath()+"/editGame?itemId="+itemId);
    }*/
}